package com.ymttest.database.mapping;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.annotations.UpdateProvider;
import org.apache.ibatis.type.JdbcType;

import com.ymatou.iapi.payment.parameter.PaymentMessageDto;
import com.ymttest.database.model.FqlCreditApply;
import com.ymttest.database.model.FqlCreditApplyExample;
import com.ymttest.database.model.FqlMember;
import com.ymttest.database.model.FqlMemberExample;
import com.ymttest.database.model.MCmbaggrement;
import com.ymttest.database.model.MPayment;
import com.ymttest.database.model.MRefund;
import com.ymttest.database.model.PaymentCheck;
import com.ymttest.database.model.PaymentCheckDetail;
import com.ymttest.database.model.payment.Paychannel;

public interface PayGateWayMapper {
	@SelectProvider(type=FqlCreditApplySqlProvider.class, method="selectByExample")
    @Results({
        @Result(column="Id", property="id", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="UserId", property="userId", jdbcType=JdbcType.INTEGER),
        @Result(column="RcOrderId", property="rcOrderId", jdbcType=JdbcType.VARCHAR),
        @Result(column="State", property="state", jdbcType=JdbcType.INTEGER),
        @Result(column="Name", property="name", jdbcType=JdbcType.VARCHAR),
        @Result(column="Identity", property="identity", jdbcType=JdbcType.VARCHAR),
        @Result(column="IdentityFrontUrl", property="identityFrontUrl", jdbcType=JdbcType.VARCHAR),
        @Result(column="IdentityReverseUrl", property="identityReverseUrl", jdbcType=JdbcType.VARCHAR),
        @Result(column="Mobile", property="mobile", jdbcType=JdbcType.VARCHAR),
        @Result(column="Work", property="work", jdbcType=JdbcType.INTEGER),
        @Result(column="Degree", property="degree", jdbcType=JdbcType.INTEGER),
        @Result(column="CreateTime", property="createTime", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="UpdateTime", property="updateTime", jdbcType=JdbcType.TIMESTAMP)
    })
    List<FqlCreditApply> selectFqlCreditApplyByExample(FqlCreditApplyExample example);
	@Select("select * from Cmbpublickey order by createtime desc limit 1")
	List<Map> selectCmbpublickey();
	//refundreq
	@Select("select * from refund where Orderid=#{orderid} order by refundid desc")
	List<MRefund> selectRefundRequestByOrderid(HashMap m);
	@Select("select * from refund where paymentid=#{paymentid} order by refundid desc")
    List<MRefund> selectRefundRequestByPaymentid(HashMap m);
	@Select("select * from refund where TradingId=#{tradeNo} order by refundid desc")
    List<MRefund> selectRefundRequestByTradeNo(HashMap m);
	
	@Select("SELECT * from mpaysynclog WHERE mpaysynclog.orderNo=#{orderNo}")
	List<Map> selectMpaysynclogByOrderNo(Map m);
	
	@Select("select * from refund where RefundId=#{rid} order by refundid desc")
	List<MRefund> selectRefundRequestByRefundId(Map m);
	/**
	 * 查询退款
	 * @param m
	 * @return
	 */
	@Select("select * from refund where RefundBatchNo=#{rno} order by refundid desc")
	List<MRefund> selectRefundRequestByRefundBatchNo(HashMap m);
    //pppayment
	  /**
     * 查询支付单
     * 
     * @param paymentId
     * @return
     * @throws InterruptedException
     */
	@Select("select * from payment where paymentid=#{paymentId}")
    public List<MPayment> selectPPPaymentByPaymentId(HashMap m);
	
	@Select("select * from payment where tradingid=#{tradingid}")
    public List<MPayment> selectPaymentByTradingid(HashMap m);
	
	@Select("select * from cmbaggrement where userid=#{uid} order by createtime desc")
	MCmbaggrement selectCmbAggrementByUId(Map m);
	@Select("select * from paychannel where paychannel.MchId=#{mid} and paychannel.DeleteStatus=0")
	Paychannel selectPaychannel(Map m);
	/**
	 * 查询退款信息 
	 * @param m {pid=PaymentId}
	 * @return
	 */
	@Select("select * from refund where PaymentId=#{pid} order by refundid desc")
	List<MRefund> selectRefundRequestByPaymentId(Map m);
	
	
	/**
	 * 更新退款状态
	 * m.put("refundno", refundno);
        m.put("status", status);
	 * @param m
	 * @return
	 */
	@Update("update refund set RefundStatus=#{status} where RefundBatchNo=#{refundno}")
	int updateRefundStatus(Map m);
	
	@Update("update refund set RefundDiscountAmt=#{rdamt} where RefundBatchNo=#{refundno}")
	int updateRefundRefundDiscountAmt(Map m);
	
	/**
	 * 更新NotifyStatus状态
	 * @param m	 
	   m.put("pid", pid);
        m.put("status", status);
	 * @return
	 */
	@Update("update payment set NotifyStatus=#{status} where paymentid=#{pid}")
	int updateNotifyStatus(Map m);
	@Update("update payment set PayStatus=#{status} where paymentid=#{pid}")
	int updatePayStatus(Map m);
	@Update("update payment set CheckStatus=#{status} where paymentid=#{pid}")
	int updateCheckStatus(Map m);
	@Update("update payment set InstProxyPaymentId= null where paymentid=#{pid}")
	int updateInstProxyPaymentIdNull(Map m);
	
	/**
	 * 查询退款信息 
	 * @param m {tid=traceid}
	 * @return
	 */
	@Select("select * from refund where RequestId=#{tid}")
	List<MRefund> selectRefundRequestByTraceId(Map m);
	//
//	@Select("select top 10 * from pp_payment where BussinessOrderId=#{bussinessOrderId}")
//	List<MPayment> selectPPPaymentByBussinessOrderId(Map m);

	@Delete("delete from cmbaggrement where userid=#{uid}")
	int deleteCmbAggrementByUId(Map m);
	
	/**
	 * 更新退款审核是否通过
	 * @param m
	 * @return
	 */
	@Update("update refund set IsCancel=#{status} where RefundBatchNo=#{refundno}")
	int updateRefundSoftDeleteFlag(Map m);
	
	//修改审核状态 0 未审核 1 审核 9 自动审核(快速退款)
	@Update("update refund set ApproveStatus=#{status} where RefundBatchNo=#{refundno}")
	int updateRefundApproveStatus(Map m);
	//修改支付时间用于90天超时退款
	@Update("update payment set paytime=#{date},createtime=#{date} where paymentid=#{pid}")
	int updatePayTimeByPid(Map m);
	// 修改招行支付的时间
	@Update("update payment set PayerEmail=#{PayerEmail} where TradingID=#{tradingId}")
	int updatePayerEmailByTradingID(Map m);
	/**
	 * 更新RefundRequest 的RefundNo
	 * @param m
	 * @return
	 */
	@Update("update refund set RefundBatchNo= #{rno} where PaymentId=#{pid}")
	int updateRefundNo(Map m);
	@Select("select  * from refund where ApproveStatus=#{approveStatus} order by createtime desc limit 20")
    List<HashMap> queryRefundrequestByApproveStatus(Integer approveStatus);
	@Select("select  * from refund where ApproveStatus=#{approveStatus} and RefundStatus=#{refundStatus} order by createtime desc limit 20")
    List<HashMap> queryRefundrequestByApproveStatusAndRefundStatus(Map m);

	List<MPayment> selectPPBussinessorderByTraceId(Map m);
	
	List<MPayment> selectPPBussinessorderByOrderId(Map m);
	/**
	 * 查询7天之前的没有对账过（CheckStatus） PayPrice 和 trading amount 一致 的数据的 PaymentId
	 * 
	 * @param m
	 *            {paytype=？,paystats=?}
	 * @return PaymentId
	 */
	String selectNoCheckPPaymentId(Map m);
	
	
//	List<RefundRequest> selectRefundRequestByPId(Map m);
	
	/**
	 * 查询发货信息 
	 * @param m {pid=PaymentId}
	 * @return
	 */
	Map selectCompensateProcessInfoByPaymentId(Map m);
	
	/**查询accounting信息
	 * @param m {bid=RefundBatchNo}
	 * @return
	 */
	Map selectPPAccoutingLogByBizno(Map m);
	/**
	 * 查询mock记录
	 * @param m {mockid=mockid}
	 * @return
	 */
	Map selectMockHttpRecordByMockId(Map m);
	
	@Select("select * from PP_Payment where BussinessOrderId in(select BussinessOrderId from PP_BussinessOrder where orderid=#{tradid})")
	List<MPayment> selectPPPaymentByTradingId(@Param("tradid")String tradid);
	
	
	//<select id="selectByPaymentIdList" resultType="com.ymatou.payment.facade.model.PaymentMessageDto" parameterType="java.util.List">
	@Select("<script>"+
	"select pm.LogType,pm.Request as ReqMessage,pm.Response as ResMessage,p.PaymentId, p.InstitutionPaymentId, p.OrderAmt - IFNULL(p.DiscountAmt, 0) AS ActPayAmt, p.PayType, p.PayStatus, p.PayTime AS TradingTime "+
	"from Payment p left join paymentmessage pm on p.PaymentId=pm.PaymentId "+
	"where p.PaymentId IN "+
	"<foreach collection=\"list\" index=\"index\" item=\"item\" open=\"(\" separator=\",\" close=\")\">"+
	"	#{item}"+
	"</foreach> order by pm.CreateTime desc"+
	"</script>")
	List<PaymentMessageDto> selectPaymentMessageDtoByPids(List<String> pid);
	
	@Select("<script>"+
			"select pm.LogType,pm.Request as ReqMessage,pm.Response as ResMessage,p.PaymentId, p.InstitutionPaymentId, p.OrderAmt - IFNULL(p.DiscountAmt, 0) AS ActPayAmt, p.PayType, p.PayStatus, p.PayTime AS TradingTime "+
			"from Payment p left join paymentmessage pm on p.PaymentId=pm.PaymentId "+
			"where p.PaymentId IN "+
			"<foreach collection=\"list\" index=\"index\" item=\"item\" open=\"(\" separator=\",\" close=\")\">"+
			"	#{item}"+
			"</foreach> order by pm.CreateTime desc"+
			"</script>")
	List<com.shop2cn.iapi.sqpayment.parameter.PaymentMessageDto> selectPaymentMessageDtoByPids2(List<String> pid);
	@Select("select * from paymentcheckdetail where paymentcheckdetail.InstPaymentId=#{InstPaymentId} and paymentcheckdetail.PaymentCheckId=#{PaymentCheckId} order by id desc")
	List<PaymentCheckDetail>selectPaymentCheckDetail(Map m);
	@Select("select * from paymentcheck where paymentcheck.PaymentChannel=#{paytype} and paymentcheck.OriginalFileName=#{fname} order by id desc")
	List<PaymentCheck>selectPaymentCheck(Map m);
	
	 /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table fqlmember
     *
     * @mbggenerated Mon Jun 24 10:19:44 CST 2019
     */
    @Insert({
        "insert into fqlmember (UserId, FqlOpenId, ",
        "State, NewUserFlag, ",
        "CreditGrantTime, TotalCredit, ",
        "AvailableCredit, UnReplayAmount, ",
        "ReplayDate, MaxOverDue, ",
        "RefreshToken, AccessToken, ",
        "CreateTime, UpdateTime)",
        "values (#{userId,jdbcType=INTEGER}, #{fqlOpenId,jdbcType=VARCHAR}, ",
        "#{state,jdbcType=INTEGER}, #{newUserFlag,jdbcType=INTEGER}, ",
        "#{creditGrantTime,jdbcType=TIMESTAMP}, #{totalCredit,jdbcType=DECIMAL}, ",
        "#{availableCredit,jdbcType=DECIMAL}, #{unReplayAmount,jdbcType=DECIMAL}, ",
        "#{replayDate,jdbcType=TIMESTAMP}, #{maxOverDue,jdbcType=INTEGER}, ",
        "#{refreshToken,jdbcType=VARCHAR}, #{accessToken,jdbcType=VARCHAR}, ",
        "#{createTime,jdbcType=TIMESTAMP}, #{updateTime,jdbcType=TIMESTAMP})"
    })
    int insert(FqlMember record);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table fqlmember
     *
     * @mbggenerated Mon Jun 24 10:19:44 CST 2019
     */
    @InsertProvider(type=FqlMemberSqlProvider.class, method="insertSelective")
    int insertFqlMemberExample(FqlMember record);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table fqlmember
     *
     * @mbggenerated Mon Jun 24 10:19:44 CST 2019
     */
    @SelectProvider(type=FqlMemberSqlProvider.class, method="selectByExample")
    @Results({
        @Result(column="UserId", property="userId", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="FqlOpenId", property="fqlOpenId", jdbcType=JdbcType.VARCHAR),
        @Result(column="State", property="state", jdbcType=JdbcType.INTEGER),
        @Result(column="NewUserFlag", property="newUserFlag", jdbcType=JdbcType.INTEGER),
        @Result(column="CreditGrantTime", property="creditGrantTime", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="TotalCredit", property="totalCredit", jdbcType=JdbcType.DECIMAL),
        @Result(column="AvailableCredit", property="availableCredit", jdbcType=JdbcType.DECIMAL),
        @Result(column="UnReplayAmount", property="unReplayAmount", jdbcType=JdbcType.DECIMAL),
        @Result(column="ReplayDate", property="replayDate", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="MaxOverDue", property="maxOverDue", jdbcType=JdbcType.INTEGER),
        @Result(column="RefreshToken", property="refreshToken", jdbcType=JdbcType.VARCHAR),
        @Result(column="AccessToken", property="accessToken", jdbcType=JdbcType.VARCHAR),
        @Result(column="CreateTime", property="createTime", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="UpdateTime", property="updateTime", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="CheckIdentity", property="checkIdentity", jdbcType=JdbcType.INTEGER),
    })
    List<FqlMember> selectFqlMemberExample(FqlMemberExample example);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table fqlmember
     *
     * @mbggenerated Mon Jun 24 10:19:44 CST 2019
     */
    @UpdateProvider(type=FqlMemberSqlProvider.class, method="updateByPrimaryKeySelective")
    int updateFqlMemberExample(FqlMember record);
	
}